package com.bajie.bjlisten.sevice;

import com.bajie.bjlisten.entity.BjSign;
import com.bajie.bjlisten.entity.BjUser;
import com.bajie.bjlisten.entity.response.BjSignResult;
import com.bajie.bjlisten.entity.response.BjUserCode;
import com.bajie.bjlisten.entity.response.CommonCode;
import com.bajie.bjlisten.enums.SignStatusEnum;
import com.bajie.bjlisten.mapper.BjSignMapper;
import com.bajie.bjlisten.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class BjSignService {

    @Autowired
    BjUserService bjUserService;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    BjSignMapper bjSignMapper;

    private static int oneDaySign = 100;
    private static int twoDaySign = 200;
    private static int threeDaySign = 300;
    private static int fourDaySign = 500;
    private static int fiveDaySign = 1000;
    private static int sixDaySign = 1500;
    private static int sevenDaySign = 2000;



    /**
     * 用户签到
     * @param userId
     * @return
     */
    public BjSignResult userSign(String userId){
        //判断用户是否存在
        Date date = new Date();
        // 获取今天的日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateString = sdf.format(date);
        log.info("-----今天是:" + dateString + "日-----");
        System.out.println("今天时间是"+dateString);

        // 获取昨天的日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        Date date1 = calendar.getTime();
        String yesterday = sdf.format(date1);
        System.out.println("昨天时间是"+yesterday);

        BjUser bjUser=bjUserService.findByUserId(userId);
        if(null==bjUser){
            return new BjSignResult(BjUserCode.BJSIGN_USER_GET_FAIL);
        }
       //用户存在
        BjSign bjSign=new BjSign();
            // 获取今天的签到信息
            BjSign todaySing = (BjSign) redisTemplate.opsForValue().get(userId + dateString);
            // 获取昨天的签到信息
            BjSign yesterdaySing = (BjSign) redisTemplate.opsForValue().get(userId + yesterday);
            //昨天是否签到
            if (yesterdaySing == null) {  //昨天未签到
                //判定今天是否签到
                if (todaySing==null){ //今天未签到
                    bjSign=new BjSign();
                    bjSign.setUser_id(userId);
                    bjSign.setSign_status(SignStatusEnum.INTERRUPT);
                    bjSign.setSign_times(1);
                    bjSign.setVirtual_coin(oneDaySign);
                    bjSign.setSign_createtime(date);
                    bjSign.setUpdateTime(date);
                    saveSign(bjSign);
                    redisTemplate.opsForValue().set(userId + dateString, bjSign, 2, TimeUnit.DAYS);
                    System.out.println("今天签到成功");
                }else//今天已经签到了
                    System.out.println("今天已经签过了，不能再签到了"+todaySing);



            }else
                {  //昨天签到
                    if (todaySing==null){ //今天未签到
                        //获取昨天的查询纪录的连续签到次数
                        bjSign=bjSignMapper.findSignYestoday(userId);
                        //根据签到次数得到今天的连续签到次数+1和对应的奖励积分
                        int sign_times=bjSign.getSign_times();

                        bjSign=new BjSign();
                        switch(sign_times){
                            case 1:
                                bjSign.setVirtual_coin(twoDaySign);//连续签到2天的奖励


                                break;
                            case 2:
                                bjSign.setVirtual_coin(threeDaySign);//连续签到3天的奖励

                                break;
                            case 3:
                                bjSign.setVirtual_coin(fourDaySign);//连续签到4天的奖励

                                break;
                            case 4:
                                bjSign.setVirtual_coin(fiveDaySign);//连续签到5天的奖励

                            case 5:
                                bjSign.setVirtual_coin(sixDaySign);//连续签到6天的奖励
                                break;
                            default:
                                bjSign.setVirtual_coin(sevenDaySign);//连续签到7天的奖励
                                break;
                        }
                        bjSign.setSign_times(sign_times+1);
                        bjSign.setSign_status(SignStatusEnum.CONTINUATION);
                        bjSign.setUser_id(userId);
                        bjSign.setSign_createtime(date);
                        bjSign.setUpdateTime(date);
                        saveSign(bjSign);
                        redisTemplate.opsForValue().set(userId + dateString, bjSign, 2, TimeUnit.DAYS);
                        }else
                        System.out.println("今天已经签过了，不能再签到了"+todaySing);


                }

        //签到
        return new BjSignResult(CommonCode.SUCCESS, (BjSign) redisTemplate.opsForValue().get(userId + dateString));
    }




    /**
     * 签到保存签到表
     * @param bjSign
     * @return
     */
    public int saveSign(BjSign bjSign){
        return bjSignMapper.saveSign(bjSign);
    }

    /**
     * 更新签到次数
     * @param userId
     * @return
     */
    public int addSignTimes(String userId){
        return bjSignMapper.addSignTimes(userId);
    }

    /**
     * 查30天的签到纪录
     * @param userId
     * @return
     */
    public List<BjSign> findSign30Days(String userId){
        BjUser bjUser=bjUserService.findByUserId(userId);
        if(null==bjUser){
            return null;
        }
        return bjSignMapper.findSign30Days(userId);
    }

    /**
     * 查询7天的签到纪录
     * @param userId
     * @return
     */
    public List<BjSign> findSign7days(String userId){
        BjUser bjUser=bjUserService.findByUserId(userId);
        if(null==bjUser){
            return null;
        }
        return bjSignMapper.findSign7days(userId);
    }

    /**
     * 查询今天的签到纪录
     * @param userId
     * @return
     */
    public BjSign findSignToday(String userId){
        return bjSignMapper.findSignToday(userId);
    }

    /**
     * 查询昨天的签到的纪录
     * @param userId
     * @return
     */
    public BjSign findSignYestoday(String userId){
        return bjSignMapper.findSignYestoday(userId);
    }

    /**
     * 查询用户的总金币
     * @param userId
     * @return
     */
    public Map<String, BigDecimal> findSumCoinByUserId(String userId){
        BjUser bjUser=bjUserService.findByUserId(userId);
        if(null==bjUser){
            return null;
        }
        return bjSignMapper.findSumCoinByUserId(userId);
    }

    /**
     * 根据提供的月份查询用户的签到纪录
     * @param userId
     * @param month
     * @return
     */
    public BjSignResult findSignByMonth(String userId,String month){
        List<BjSign> bjSignList= bjSignMapper.findSignByMonth(userId, month);
        if(StringUtils.isNotEmpty(bjSignList)&&bjSignList.size()>0)
            return new BjSignResult(CommonCode.SUCCESS, bjSignList);

        return new BjSignResult(BjUserCode.BJEBOOK_GET_FAIL);
    }



}
